package org.bookie.web.pages;

import java.util.List;

import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.BeanPropertySelectionModel;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.bookie.framework.CommonBasePage;
import org.bookie.framework.components.PeriodComponent;
import org.bookie.model.Bet;
import org.bookie.model.Category;
import org.bookie.model.Event;
import org.bookie.model.services.BetDAO;
import org.bookie.model.services.CategoryDAO;

/**
 * Page listing the results of the {@link Event}s in the past.
 * These {@link Event}s cannot be used for betting anymore. 
 * 
 * @author Igor Drobiazko
 *
 */
public abstract class Results extends CommonBasePage implements PageBeginRenderListener{
	/**Identifier of the client component containing the period component.*/
	public static final String PERIOD = "period";
	
	public void pageBeginRender(PageEvent event) {
		PeriodComponent period = (PeriodComponent) getComponent(Results.PERIOD);
		List<Bet> bets = getBetDAO().findFinalized(period.getPeriod(), getCategory());
		setBets(bets);
	}
    /**
     * Get the selection model for {@link Category} options 
     * @return instance of IPropertySelectionModel
     */
	public IPropertySelectionModel getCategories(){
		return new BeanPropertySelectionModel(getCategoryDAO().findAll(), "name");
	}
	
	/**
	 * Injected {@link Bet} service
	 * @return instance of BetDAO
	 */
	@InjectObject("service:bookie.model.BetDAO")
	public abstract BetDAO getBetDAO();
	
	/**
	 * Injected {@link Category} service
	 * @return instance of CategoryDAO
	 */
	@InjectObject("service:bookie.model.CategoryDAO")
	public abstract CategoryDAO getCategoryDAO();
	
	/**
	 * Set the current {@link Bet} of the iteration 
	 * @param bet Bet to set
	 */
    public abstract void setCurrentBet(Bet bet);
	
	/**
	 * Get the selected {@link Category}
	 * @return instance of Category
	 */
	public abstract Category getCategory();
	
	/**
	 * Get the list of {@link Bet}s to display.
	 * @return list of Bets
	 */
	public abstract List<Bet> getBets();
	/**
	 * Set the list of {@link Bet}s to display.
	 * @param bets bets to set
	 */
	public abstract void setBets(List<Bet> bets);

}
